package com.crr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.crr.bean.ResultBean;
import com.crr.bean.User;
import com.crr.config.EvnConfig;
import com.crr.config.RestConfig;
import com.crr.config.WXConfig;
import com.crr.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.scene.shape.VLineTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@Api(tags = "用户操作")
@RequestMapping("/api/user")
public class UserController extends BaseController {
    @Autowired
    UserService userService;
    @Autowired
    WXConfig wxConfig;
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/session")
    public ResultBean getSession(@RequestParam( required = true) String code) {
        String url = EvnConfig.code2Session + "?appid=" + wxConfig.getAppid() + "&secret=" + wxConfig.getSecret() + "&js_code=" + code + "&grant_type=" + wxConfig.getGrant_type();
        String res = restTemplate.getForObject(url, String.class);
        Map map = JSONObject.parseObject(res, Map.class);
        return success("会话信息", map);

    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public ResultBean register(@RequestBody User user) {
        int i = userService.saveEntity(user);
        return saveResult(i);
    }

    @GetMapping("/get")
    @ApiOperation("查询个人中心")
    public ResultBean query(@RequestParam(value = "id", required = true) int id) {
        User entity = userService.getEntity(id);
        return success("个人信息", entity);
    }
}
